<?php
/*
 * Joomla! 1.5 component ArticleCashRewards
 *
 * @version $Id: rejected.php 2009-05-10 01:47:01 svn $
 * @author Maverick
 * @package controllers
 * @subpackage ArticleCashRewards
 * @license GNU/GPL
 * Website: http://www.boddunan.com
 *
 * Review and approve the articles and assign points and cash rewards.
 * Users can see their articles and the points/cash rewards assigned in the front-end module.
 */

defined('_JEXEC') or die();

class ArticleCashRewardsControllerReferrals extends ArticleCashRewardsController
{

	function __construct(){
		parent::__construct();
        global $mainframe, $acr_config;
        $acr_config = $mainframe->getUserState( SESSION_ACR_CONFIG );

        if(!isset($acr_config)){
            $model = & $this->getModel('config');
            $configt = $model->getConfiguration();

            foreach($configt as $ct){
                $acr_config[$ct->config_name] = $ct->config_value;
            }
            $mainframe->setUserState( SESSION_ACR_CONFIG, $acr_config );
        }
        $this->registerTask( 'pending','viewPendingReferrals' );
        $this->registerTask( 'reviewed','viewReviewedReferrals' );
        $this->registerTask( 'recalcpage','recalcPage' );
        $this->registerTask( 'recalculate','recalculate' );
        $this->registerTask( 'manageRules','manageRules' );
        $this->registerTask( 'addLevel','addRule' );
        $this->registerTask( 'removeLevel','removeRule' );
        $this->registerTask( 'publishLevel','publishRule' );
        $this->registerTask( 'unpublishLevel','unpublishRule' );
        $this->registerTask( 'approvePayments','approvePayments' );
        $this->registerTask( 'disapprovePayments','disApprovePayments' );
        $this->registerTask( 'cPanel','cPanel' );
	}
    
    function approvePayments(){
        $model = & $this->getModel('referrals');
        $msg = '';
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
        $link = 'index.php?option=com_articlecashrewards&controller=referrals&task=pending';

		if (count($cid) < 1) {
			$msg =  'Select an item to publish';
			$this->redirect($link, $msg);
		}

        if($model->setReferralApprovalStatus(1, $cid)){
            $model = & $this->getModel('reports');
            $uids = $model->getUserIds($cid, 2);
            $model->recalculateEarnings($uids);
            $msg = 'Successfully approved.';
        }else{
            $msg = $model->getError();
        }
        $link = 'index.php?option=com_articlecashrewards&controller=referrals&task=pending';
        $this->setRedirect($link, $msg);
    }

    function disApprovePayments(){
        $model = & $this->getModel('referrals');
        $msg = '';
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
        $link = 'index.php?option=com_articlecashrewards&controller=referrals&task=pending';

		if (count($cid) < 1) {
			$msg =  'Select an item to unpublish';
			$this->redirect($link, $msg);
		}

        if($model->setReferralApprovalStatus(2, $cid)){
            $model = & $this->getModel('reports');
            $uids = $model->getUserIds($cid, 2);
            $model->recalculateEarnings($uids);
            $msg = 'Successfully disapproved.';
        }else{
            $msg = $model->getError();
        }
        $link = 'index.php?option=com_articlecashrewards&controller=referrals&task=pending';
        $this->setRedirect($link, $msg);
    }
    
    function viewPendingReferrals(){
        $view = & $this->getView('referrals', 'html');
		$model = & $this->getModel('referrals');
		$view->setModel($model, true);
        $view->setLayout('pending');
        $view->display();
    }

    function viewReviewedReferrals(){
        $view = & $this->getView('referrals', 'html');
		$model = & $this->getModel('referrals');
		$view->setModel($model, true);
        $view->setLayout('reviewed');
        $view->display();
    }

    function manageRules(){
		$view = & $this->getView('referrals', 'html');
		$model = & $this->getModel('referrals');
		$view->setModel($model, true);
        $view->setLayout('levels');
        $view->display();
    }
    
	function addRule(){
		$view = & $this->getView('referrals', 'html');
		$model = & $this->getModel('referrals');
        $msg = '';
        if($model->addLevel()){
            $msg = 'Successfully added new rule.';
        }else{
            $msg = $model->getError();
        }
        $link = 'index.php?option=com_articlecashrewards&controller=referrals&task=manageRules';
        $this->setRedirect($link, $msg);
	}

    function recalcPage(){
		$view = & $this->getView('referrals', 'html');
		$model = & $this->getModel('referrals');
		$view->setModel($model, true);
        $view->setLayout('recalcpage');
        $view->display();
    }

	function recalculate(){
		$view = & $this->getView('referrals', 'html');
		$model = & $this->getModel('referrals');
        $msg = '';
        if($model->processReferralPayments()){
            $msg = 'Successfully processed referral payments. Please approve them.';
        }else{
            $msg = $model->getError();
        }
        $link = 'index.php?option=com_articlecashrewards&controller=referrals&task=pending';
        $this->setRedirect($link, $msg);
	}

	function removeRule(){
		$model = & $this->getModel('referrals');
        $msg = '';
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
        $link = 'index.php?option=com_articlecashrewards&controller=referrals&task=manageRules';

		if (count($cid) < 1) {
			$msg =  'Select an item to delete';
			$this->redirect($link, $msg);
		}

        if($model->removeRules($cid)){
            $msg = 'Success.';
        }else{
            $msg = $model->getError();
        }
        $link = 'index.php?option=com_articlecashrewards&controller=referrals&task=manageRules';
        $this->setRedirect($link, $msg);
	}

	function publishRule(){
		$model = & $this->getModel('referrals');
        $msg = '';
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
        $link = 'index.php?option=com_articlecashrewards&controller=referrals&task=manageRules';

		if (count($cid) < 1) {
			$msg =  'Select an item to publish';
			$this->redirect($link, $msg);
		}

        if($model->setRuleStatus(1, $cid)){
            $msg = 'Success.';
        }else{
            $msg = $model->getError();
        }
        $link = 'index.php?option=com_articlecashrewards&controller=referrals&task=manageRules';
        $this->setRedirect($link, $msg);
	}

	function unpublishRule(){
		$model = & $this->getModel('referrals');
        $msg = '';
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
        $link = 'index.php?option=com_articlecashrewards&controller=referrals&task=manageRules';
        
		if (count($cid) < 1) {
			$msg =  'Select an item to unpublish';
			$this->redirect($link, $msg);
		}
        
        if($model->setRuleStatus(0, $cid)){
            $msg = 'Success.';
        }else{
            $msg = $model->getError();
        }
        $link = 'index.php?option=com_articlecashrewards&controller=referrals&task=manageRules';
        $this->setRedirect($link, $msg);
	}

    function cPanel(){
        $link = 'index.php?option=com_articlecashrewards&controller=cpanel';
        $this->setRedirect($link);
    }
}
?>
